//
//  GHSuggestViewController.m
//  GodHorses
//
//  Created by Mac on 2017/11/16.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "OESuggestViewController.h"
#import "STTextView.h"
#import "UITextField+STInputLimit.h"
#import "STRadioButton.h"
#define MAX_NUM 200
@interface OESuggestViewController ()
@property(nonatomic, strong) STTextView                     *textView;
@property(nonatomic, strong) UITextField                    *textFiled;
@property(nonatomic, strong) STRadioButton                     *radioButton;/**< <##> */

@property(nonatomic, strong) TMImageAutoChoseView                     *imageChoseView;/**<  */


@end

@implementation OESuggestViewController
#pragma mark --vc 生命周期
- (void)viewDidLoad{
    [super viewDidLoad];
    self.tableView.backgroundColor = OE_GrayColor;
    self.title = OESTRING(@"FEEDBACK");
    __weak typeof(self) weakSelf =  self;
    self.imageChoseView = [[TMImageAutoChoseView alloc] initWithFrame:CGRectMake(10, self.textView.bottom+10, UIScreenWidth - 24 -20, 100)];
    [self.imageChoseView setFrameDidChangedHandle:^(TMImageAutoChoseView *autochoseView) {
        [weakSelf configTableFooter];
    }];
    self.imageChoseView.maxCount = 9;
    self.imageChoseView.addImage = [UIImage imageNamed:@"200×200添加照片"];
    self.textFiled = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 44)];
    self.textFiled.backgroundColor = UIColor.whiteColor;
    self.textFiled.placeholder = @"FILL IN THE MOBILE";
    self.textFiled.leftViewMode = UITextFieldViewModeAlways;
    self.textFiled.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, 44)];
    self.textFiled.keyboardType = UIKeyboardTypeNumberPad;
    
    //[self st_setRightItemWithTitle:@"保存" titleColor:[UIColor whiteColor]];
    [self configSubView];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}

#pragma mark --subView
- (void)configSubView{
    [self configTableHeader];
    [self configTableFooter];
    [self st_autoAdjustAllResponder];
}
- (void)configTableHeader{
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    header.backgroundColor =  UIColor.whiteColor;
    ({
        STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(15, 0, 200, 44)
                                                         text:@"FeedBack Question:"
                                                    textColor:FirstTextColor
                                                         font:15
                                                  isSizetoFit:NO
                                                textAlignment:NSTextAlignmentLeft];
        [header addSubview:titleLable];
        [NSString lableAutoAdjustWitdhWithLabel:titleLable];
        self.radioButton = [[STRadioButton alloc] initWithFrame:CGRectMake(titleLable.left, titleLable.bottom, UIScreenWidth - titleLable.right, 88)
                                                         titles:@[@"Shop question",@"Sort question"]
                                                       itemSize:CGSizeMake(UIScreenWidth - 30, 44)
                                                      itemInset:UIEdgeInsetsMake(0, 20, 0, 0)];
        [header addSubview:self.radioButton];
        self.radioButton.chosedIndex = 0;
        UIView * line = [[UIView alloc] initWithFrame:CGRectMake(15, 43, UIScreenWidth - 30, 0.5)];
        line.backgroundColor = TM_lineColor;
        [header addSubview:line];
    });
    
    
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(15, self.radioButton.bottom, 200, 44)
                                                     text:@"Description:"
                                                textColor:FirstTextColor
                                                     font:15
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [header addSubview:titleLable];
    [NSString lableAutoAdjustWitdhWithLabel:titleLable];
    
    
    self.textView = [[STTextView alloc] initWithFrame:CGRectMake(15, titleLable.bottom, UIScreenWidth - 30, 180)];
    self.textView.placeholder = @"Please input feedback information within 200 words";
    self.textView.placeholderColor = SecendTextColor;
    self.textView.backgroundColor = self.tableView.backgroundColor;
    [header addSubview:self.textView];
    
    
    
    header.height = self.textView.bottom + 10;
    self.tableView.tableHeaderView = header;
}
- (void)configTableFooter{
    __weak typeof(self) weakSelf =  self;
    UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    footer.backgroundColor = self.tableView.backgroundColor;
    
    UIView * whietView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    whietView.backgroundColor = UIColor.whiteColor;
    [footer addSubview:whietView];
    
    self.imageChoseView.backgroundColor = UIColor.whiteColor;
    self.imageChoseView.top = 10;
    [whietView addSubview:self.imageChoseView];
    whietView.height = self.imageChoseView.bottom + 10;
    
    
    UIView * line = [[UIView alloc] initWithFrame:CGRectMake(0, whietView.bottom, UIScreenWidth, 10)];
    line.backgroundColor = self.tableView.backgroundColor;
    [footer addSubview:line];
    
    self.textFiled.top = line.bottom;
    [footer addSubview:self.textFiled];
    
    
    STButton * confimButton = [[STButton alloc] initWithFrame:CGRectMake(30, self.textFiled.bottom + 30, UIScreenWidth - 60, 44)
                                                        title:@"COMPLETE"
                                                   titleColor:[UIColor whiteColor]
                                                    titleFont:18
                                                 cornerRadius:22
                                              backgroundColor:OE_LIMITE_COLOR
                                              backgroundImage:nil
                                                        image:nil];
    [confimButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedCommitButton];
    }];
    confimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    footer.height = confimButton.bottom + 20;
    [footer addSubview:confimButton];
    self.tableView.tableFooterView = footer;
}
#pragma mark --Action Method
- (void)onSelectedCommitButton{
    if (!self.textView.text.length) {
        [SVProgressHUD showErrorWithStatus:@"Please enter your Suggestions"];
        return;
    }
    if (!self.textFiled.text.length) {
        [SVProgressHUD showErrorWithStatus:@"Please enter your contact information"];
        return;
    }
    [self sendFeadbacekRequest];
}
#pragma mark --NetWork Method
- (void)sendFeadbacekRequest{
    [SVProgressHUD showWithStatus:@"Please later"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php?m=Front&c=User&a=feedBack"];
    NSString * token = STUserManger.defult.loginedUser.token;
    if (token.length) {
        [dic setObject:token forKey:@"user_token"];
    }
    [dic setObject:@(self.radioButton.currentChosedIndex+1) forKey:@"feed_type"];
    if (self.textView.text.length) {
        [dic setObject:self.textView.text forKey:@"content"];
    }
    if (self.textFiled.text.length) {
        [dic setObject:self.textFiled.text forKey:@"mobile"];
    }
    NSArray * array = self.imageChoseView.allImages;
    NSMutableArray * files = NSMutableArray.new;
    for (NSInteger i = 0 ; i < array.count; i ++) {
        [files addObject:[NSString stringWithFormat:@"files[%ld]",i]];
    }
    [[STNetWrokManger defaultClient] imagesRequestWithPath:url parameters:dic images:array imageNames:files progress:^(NSProgress *progress) {
        
        CGFloat downprogress = 1.0 * progress.completedUnitCount / progress.totalUnitCount;
        [SVProgressHUD showProgress:downprogress];
        
    } success:^(NSURLSessionDataTask *operation, id responseObject) {
        [SVProgressHUD dismiss];
        [SVProgressHUD showSuccessWithStatus:@"We have received your suggestion"];
        
    } failure:^(NSString *stateCode, STError *error, NSError *originError) {
        [SVProgressHUD dismiss];
        [SVProgressHUD showErrorWithStatus:@"cmmit error"];
    }];
    
   
}
@end

